package de.baumann.browser.views.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.Gravity
import android.widget.Toast
import de.baumann.browser.OdinGlideModule
import de.baumann.browser.R
import de.baumann.browser.adapter.Fene344Adapter
import de.baumann.browser.api.net.vo.duobao.DuoBao
import kotlinx.android.synthetic.main.activity_create_node.*
import kotlinx.android.synthetic.main.dialog_duobao_buy.*
import kotlinx.android.synthetic.main.item_power.*

class DuoBaoBuyDialog(context: Context, var duoBaoCallBack: DuoBaoCallBack) : BaseDialog(context) {
    override fun laoutId(): Int {
        return R.layout.dialog_duobao_buy
    }

    private var fene = 1

    override fun initView() {

        val fene344 = Fene344Adapter()
        fene344.setNewData(arrayListOf("5", "20", "50", "包尾"))
        rv344.adapter = fene344
        fene344.setOnItemClickListener { _, _, position ->
            fene344.setp(position)
            when (position) {
                0 -> {
                    edFene.setText("5")
                }
                1 -> {
                    edFene.setText("20")
                }

                2 -> {
                    edFene.setText("50")
                }

                3 -> {
                    edFene.setText(duoBao.residuePartNum.toString())
                }
            }
        }

        close.setOnClickListener {
            dismiss()
        }

        feneJian.setOnClickListener {
            fene344.setp(-1)
            fene = edFene.text.toString().toInt()
            if (fene <= 1) {
                return@setOnClickListener
            }
            fene -= 1
            edFene.setText("$fene")
        }

        feneJia.setOnClickListener {
            fene344.setp(-1)
            fene = edFene.text.toString().toInt()
            if (fene >= duoBao.residuePartNum) {
                return@setOnClickListener
            }
            fene += 1
            edFene.setText("$fene")
        }

        edFene.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
                if (p0 == null || p0.isEmpty()) {
                    return
                }
                val inputfene = p0.toString().toInt()
                if (inputfene > duoBao.residuePartNum) {
                    Toast.makeText(mContext, "份额不足", Toast.LENGTH_SHORT).show()
                    edFene.setText("$fene")
                    fene344.setp(-1)
                } else {
                    fene = inputfene
                    setZhiFuJine(fene)
                }

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }
        })

        btnLiJiZhiFu.setOnClickListener {
            duoBaoCallBack.callback(fene.toString(), (fene * duoBao.onlyvalue).toString())
        }


    }

    private lateinit var duoBao: DuoBao

    @SuppressLint("SetTextI18n")
    fun setData(duoBao: DuoBao) {
        this.duoBao = duoBao
        zongFene.text = "总需份额${duoBao.totalValue}份"
        shengYuFene.text = "剩余 ${duoBao.residuePartNum} 份"
        OdinGlideModule.load(mContext, duoBao.thumbnail, buyCover, R.drawable.shape_cover)
        setZhiFuJine(fene)
        text344.text = "参与份额(一份${duoBao.onlyvalue}ODIN)"
    }

    private fun setZhiFuJine(num: Int) {
        val jine = num * duoBao.onlyvalue
        zhifujine.text = jine.toString()
    }

    override fun gravity(): Int {
        return Gravity.BOTTOM
    }
}

interface DuoBaoCallBack {
    fun callback(number: String, jine: String)
}